package com.qxzn.app.api.greendao;

import android.content.Context;

import com.qxzn.app.api.greendao.bean.DaoMaster;
import com.qxzn.app.api.greendao.bean.DaoSession;
import com.qxzn.app.util.SharePreferenceUtil;

import org.greenrobot.greendao.database.Database;

public class SQLiteDbUtil {
    private static SQLiteDbUtil sqLiteDbUtil;
    private Context context;
    private DaoSession dao;

    private SQLiteDbUtil(Context context) {
        this.context = context;
    }

    public static SQLiteDbUtil getInstance(Context context) {
        if (sqLiteDbUtil == null) {
            synchronized (SQLiteDbUtil.class) {
                if (sqLiteDbUtil == null) {
                    sqLiteDbUtil = new SQLiteDbUtil(context.getApplicationContext());
                }
            }
        }
        return sqLiteDbUtil;
    }

    public DaoSession getDb() {
        if (dao == null) {
            MySQLiteOpenHelper helper = new MySQLiteOpenHelper(context, "robot_" + SharePreferenceUtil.getDeviceId(context) + SharePreferenceUtil.getUserName(context));
            Database db = helper.getWritableDb();
            dao = new DaoMaster(db).newSession();
        }
        return dao;
    }
}
